package com.dimples.dd.common.pojo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dimples.dd.common.result.ResultCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Schema(description = "分页结果")
@Data
public final class PageResult<T> implements Serializable {

    private String code;

    private Data<T> data;

    private String msg;

    public static <T> PageResult<T> success(IPage<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());

        Data<T> data = new Data<>();
        data.setRecords(page.getRecords());
        data.setTotal(page.getTotal());
        data.setCurrent(page.getCurrent());
        data.setSize(page.getSize());

        result.setData(data);
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    public static <T> PageResult<T> success(List<T> list) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());

        Data<T> data = new Data<>();
        data.setRecords(list);
        data.setTotal((long) list.size());
        data.setCurrent(1L);
        data.setSize(10L);

        result.setData(data);
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    public static <T> PageResult<T> success(List<T> list, long total) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());

        Data<T> data = new Data<>();
        data.setRecords(list);
        data.setTotal(total);
        data.setCurrent(1L);
        data.setSize(10L);

        result.setData(data);
        result.setMsg(ResultCode.SUCCESS.getMsg());
        return result;
    }

    @lombok.Data
    public static class Data<T> {

        @Schema(description = "当前页数", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long current;

        @Schema(description = "页内大小", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long size;

        @Schema(description = "总量", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long total;

        @Schema(description = "数据", requiredMode = Schema.RequiredMode.REQUIRED)
        private List<T> records;

    }

}
